
import axios from 'axios';
import type { CommonRes } from './commonType'
import { isUndefined, isNull } from 'lodash';


export interface PersonneStatisticsQuery {
  page: number,
  size: number,
  id?: string,
  uid?: string,
  name?: string,
  start_time: string,
  end_time: string,
  plan_name?: string,
}

export interface ProductStatisticsQuery {
  page?: number,
  size?: number,
  start_time: string,
  end_time: string,
  fuzzy_search?: string
}


export interface ProductReturnItem {
  id: number,
  date: string,
  plan_name: string,
  product_clue_num: number,
  product_id: number,
  product_name: string
}



/**
 * 获取个人统计
 * @param data 
 * @returns 
 */
export function getPersonneStatistics(data: PersonneStatisticsQuery) {
  return axios.post<CommonRes>('/organization/finance/personnellistpage', data);
}


/**
 * 获取产品统计
 * @param data 
 * @returns 
 */
export function getProductStatistics(data: ProductStatisticsQuery) {
  return axios.post<CommonRes>('/organization/finance/productStats', data).then(({ data }: any)=>{
    if(!isUndefined(data?.datalist?.data) && !isNull(data?.datalist?.data)){
      let arrList : ProductReturnItem[] = [];
      const dataArr = data?.datalist?.data;
      dataArr.forEach((item: any) => {
        arrList.push({
          id: item?.id,
          date: item?.date,
          plan_name: item?.product?.plan_name,
          product_clue_num: item?.product_clue_num,
          product_id: item?.product_id,
          product_name: item?.product?.name,
        })
      });
      return { data: arrList, total: data?.datalist?.total }
    }  
    return { data: [], total: 0 }
  });
}


